package com.smallA.service.Impl;

import com.smallA.constant.DataConstant;
import com.smallA.dao.GradeMapper;
import com.smallA.dao.TestMapper;
import com.smallA.dao.TopicTypeMapper;
import com.smallA.po.*;
import com.smallA.service.TestService;
import com.smallA.utils.DataUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 君未洋
 */
public class TestServiceImpl implements TestService {

    private TestMapper testMapper;
    private TopicTypeMapper topicTypeMapper;
    private DataUtils dataUtils;

    public void setGradeMapper(GradeMapper gradeMapper) {
        this.gradeMapper = gradeMapper;
    }

    private GradeMapper gradeMapper;

    public void setTopicTypeMapper(TopicTypeMapper topicTypeMapper) {
        this.topicTypeMapper = topicTypeMapper;
    }

    public void setTestMapper(TestMapper testMapper) {
        this.testMapper = testMapper;
    }

    public void setDataUtils(DataUtils dataUtils) {
        this.dataUtils = dataUtils;
    }

    @Override
    public List<Test> selectAllTest() {
        return testMapper.selectAllTest();
    }

    @Override
    public Test selectTestById(Integer id) {
        return testMapper.selectTestById(id);
    }

    @Override
    public boolean windingTest(Map map, Integer sid, Integer gid) {
        if (dataUtils.examineNumber(sid, gid)) {
            return false;
        }
        Set<String> strings = map.keySet();
        //获取下所有的key
        Answer answer = new Answer();
        answer.setSid(sid);
        answer.setGid(gid);
        for (String string : strings) {
            String[] split = string.split("#");
            if (split.length != 2) {
                continue;
            }
            answer.setAnswer(map.get(string).toString());
            answer.setQid(Integer.parseInt(split[1]));
            String strAnswer = Arrays.toString((Object[]) map.get(string));
            answer.setAnswer(strAnswer.substring(1, strAnswer.length() - 1));

            if (DataConstant.TEST_CHOICE.equals(split[0])) {
                answer.setType(1);
            } else if (DataConstant.TEST_FILL.equals(split[0])) {
                answer.setType(2);
            } else if (DataConstant.TEST_SHORT_ANSWER.equals(split[0])) {
                answer.setType(3);
            }
            testMapper.insertAnswer(answer);
        }
        return true;
    }

    @Override
    public List<TopicType> selectAllTopicTpe() {
        return topicTypeMapper.selectAllTopicType();
    }

    @Override
    public boolean insertTest(Date beginDate, Integer examId, Integer useTime, String context) {
        if (dataUtils.examineNumber(examId, useTime)) {
            return false;
        }
        Test test = new Test(0, examId, context, beginDate, useTime);
        return testMapper.insertTest(test) > 0;
    }

    @Override
    public Integer selectTestByGradeId(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return null;
        }
        return testMapper.selectTestByGradeId(gid);
    }

    @Override
    public boolean markGrade(Float[] fills,Float[] shorts, Exam exam,Integer gid) {
        if(dataUtils.examineNumber(gid)){
            return false;
        }
        Map<String, Object> map = exam.getQuestion();
        List<Choice> choiceList = (List<Choice>) map.get(DataConstant.QUESTION_CHOICE);
        List<Fill> fillList = (List<Fill>) map.get(DataConstant.QUESTION_FILL);
        List<ShortAnswer> shortList = (List<ShortAnswer>) map.get(DataConstant.QUESTION_SHORT_ANSWER);

        if(choiceList.size()==0&&dataUtils.examineFloatArray(fills)&&dataUtils.examineFloatArray(shorts)){
            return false;
        }
        float score = 0;
        //选择题答案
        for (int i = 0; i < choiceList.size(); i++) {
            //答案正确
            if(choiceList.get(i).getAnswer()==choiceList.get(i).getStudentAnswer()){
                score += choiceList.get(i).getScore();
            }
        }
        for (int i = 0; i < fillList.size(); i++) {
            float scores = fills[i];
            if(scores<0){
                return false;
            }
            score+= scores;
        }
        for (int i = 0; i < shortList.size(); i++) {
//            float s = Float.parseFloat(request.getParameter(String.valueOf(shortList.get(i).getId())))

            float scores = shorts[i];
            if(scores<0){
                return false;
            }
            score+= scores;
        }
        gradeMapper.updateGradeScore(gid,score);
        return true;
    }


}
